package lch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lch.dao.CheckGroupDao;
import lch.entity.PageResult;
import lch.entity.QueryPageBean;
import lch.pojo.CheckGroup;
import lch.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author lch
 * @date 2019-12-20 18:48
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkitemIds, checkGroupId);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //编辑左侧
        checkGroupDao.edit(checkGroup);
        //删除原先关联
        checkGroupDao.deleteAssoication(checkGroup.getId());
        //重新建立关系
        this.setCheckGroupAndCheckItem(checkitemIds, checkGroup.getId());
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        if (checkGroup!=null){
            //清理t_checkgroup_checkitem中间表(检查组和检查项)
            checkGroupDao.deleteAssoication(id);
            //清理t_setmeal_checkgroup中间表(检查组和套餐)
            checkGroupDao.deleteSetmealAndGroupByGroupId(id);
            //删除检查组
            checkGroupDao.deleteById(id);
        }
    }

    public void setCheckGroupAndCheckItem(Integer[] checkitemIds, Integer checkGroupId) {
        if (checkitemIds != null && checkitemIds.length > 0) {//加个判断
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}